<fieldset class="layui-elem-field layui-field-title">
  <legend>用户管理列表</legend>
</fieldset>

<script type="text/html" id="bar">
{{#
	if(d.status == 1){
}}
<a class="layui-btn  layui-btn-sm" lay-event="recover">恢复</a>
{{#
	}
}}
{{#
	if(d.status == 0){
}}
<a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="delete">禁用</a>
{{#
	}
}}
	<a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="edit">编辑</a>
</script>
<table id="list" lay-filter="list"></table>
<script type="text/javascript">
	layui.use(["table","form"],function(){
		var table=layui.table;
		var form=layui.form;
		table.render({
			id:"list",
			elem:'#list',
			url:'http://127.0.0.1:8888/api/users',
			where:{parent:0},
			method:'get',
			height:600,
			page:true,
			cols:[[
				{field:'id',title:'ID',width:50},
				{field:'username',title:'用户名',width:100},
				{field:'head',title:'头像'},
				{field:'realname',title:'真实姓名'},
				{field:'phone',title:'电话'},
				{field:'money',title:'余额'},
				{field:'status',title:'状态',width:70,templet:function(d){
					return ['正常','禁用'][d.status];
				}},
				{title:'操作',toolbar:'#bar',width:140,fixed:'right' }
			]],		
			parseData:function(res){
				return{
					"code": 0,
					"msg": res.msg,
					"count": res.data.total,
					"data":res.data.list
				}
			},
			done: function(res, curr, count){
				// 固定列高度适应
				$(".layui-table-main  tr").each(function (index ,val) {
					$($(".layui-table-fixed .layui-table-body tbody tr")[index]).height($(val).height());
				});
			}
		});
		
		
		/**
		 * 绑定删除和编辑的事件
		 */
		table.on('tool(list)',function(obj){
			if(obj.event == 'delete'){
				// 确认删除
				layer.confirm('果真要禁用这一个用户吗？',function(index){
					
					layer.close(index);
					// 执行删除操作
					// ID在哪里 obj.data.id
					$.ajax({
						url:'http://127.0.0.1:8888/api/users',
						type:'delete',
						data:{id: obj.data.id},
						dataType: 'json',
						success:function(data){
							if(data.status==204){
								//删除成功
								layer.msg("禁用成功")
				
							}else if(data.status == 404){
								layer.msg(data.msg)
							}
							// 刷新表格
							table.reload('list')
						},
						error:function(){
							
						}
					});
				});
			} else if(obj.event == 'recover') {
				// 确认恢复
				layer.confirm('果真要恢复这个用户吗？',function(index){
					
					layer.close(index);
					// 执行删除操作
					// ID在哪里 obj.data.id
					$.ajax({
						url:'http://127.0.0.1:8888/api/users',
						type:'delete',
						data:{id: obj.data.id},
						dataType: 'json',
						success:function(data){
							if(data.status==204){
								//删除成功
								layer.msg("恢复成功")
				
							}else if(data.status == 404){
								layer.msg(data.msg)
							}
							// 刷新表格
							table.reload('list')
						},
						error:function(){
							
						}
					});
				});
			} 
		
		return false;
		
	});
	});
	
</script>
